<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="ruleForm" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="ruleForm.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="ruleForm.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="ruleForm.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="ruleForm.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in Types.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="ruleForm.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="ruleForm.departmentName"
          style="width:50%"
          placeholder="请选择部门"
          @focus="departmentNameFocus"
        />
        <!-- 部门树形 -->
        <div class="tree-box">
          <el-tree :data="treeData" :props="{ label: 'name' }" @node-click="selectNode" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="ruleForm.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Types from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
// 引入静态常量
import { transTreeData } from '@/utils/transTreeData'
import { addEmployee } from '@/api/employees'
export default {
  name: 'AddEmp',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单
      ruleForm: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 校验
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      // 聘用形式
      Types,
      // 定义数组接收树形数据
      treeData: []
    }
  },
  mounted() {
    // this.hGetDepartments()
  },
  methods: {
    // 关闭弹层
    closeDialog() {
      // 关闭
      this.$emit('close-dialog')
      // 清空
      this.ruleForm = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      this.$refs.form.resetFields()
    },
    // 渲染部门数据
    async hGetDepartments() {
      // 获取部门数据
      const res = await getDepartments()
      // 树形转换
      this.treeData = transTreeData(res.depts)
    },
    // 节点被点击时触发
    selectNode(data) {
      // console.log('data属性的数组中该节点所对应的对象:', data)
      // console.log('节点对应的 Node:', node)
      // console.log('节点组件本身:', a) 组件实例对象
      // data表示当前点击节点对应的完整数据
      // 把点击节点的name树形回西安到用来负责部门的字段身上
      // 如果当前点击节点下还有子节点 点击只展开
      // 如果没有子节点 选中
      if (data.children.length > 0) {
        return
      } else {
        this.ruleForm.departmentName = data.name
      }
    },
    // 聚焦时获取
    departmentNameFocus() {
      // 获取部门数据
      this.hGetDepartments()
    },
    // 添加员工
    submitAdd() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          // 发请求
          await addEmployee(this.ruleForm)
          // 提示用户
          this.$message.success('新增成功')
          // 关闭
          this.$emit('close-dialog')
          // 通知父组件更新数据
          this.$parent.getEmployeeList()
        }
      })
    }
  }

}
</script>

<style>

</style>
